package com.cskaoyan.order.service.impl;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.dto.AddressDto;
import com.cskaoyan.order.converter.OrderConverter;
import com.cskaoyan.order.converter.OrderItemConverter;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.OrderShipping;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.OrderShippingMapper;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.OrderListRequest;
import com.cskaoyan.order.service.OrderQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 2731235517@qq.com 谢畅
 * @since 2023/01/10 13:58
 */
@Service
@Transactional
public class OrderQueryServiceImpl implements OrderQueryService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderShippingMapper orderShippingMapper;
    @Autowired
    OrderConverter orderConverter;

    @Autowired
    OrderItemConverter orderItemConverter;

    @Override
    public OrderListResponse orderList(OrderListRequest orderListRequest) {
        OrderListResponse orderListResponse = new OrderListResponse();

        Example orderExample = new Example(Order.class);
        orderExample.createCriteria().andEqualTo("userId",orderListRequest.getUserId());
        List<Order> orders = orderMapper.selectByExample(orderExample);
        List<OrderDetailInfo> data = new LinkedList<>();

        for (Order order : orders) {
            OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order);
            data.add(orderDetailInfo);
        }

        for (OrderDetailInfo orderDetailInfo : data) {
            Example orderItemExample = new Example(OrderItem.class);
            orderItemExample.createCriteria().andEqualTo("orderId", orderDetailInfo.getOrderId());
            List<OrderItem> orderItems = orderItemMapper.selectByExample(orderItemExample);
            List<OrderItemDto> orderItemDtoList = orderConverter.item2dto(orderItems);
            orderDetailInfo.setOrderItemDto(orderItemDtoList);

            Example orderShippingExample = new Example(OrderShipping.class);
            orderShippingExample.createCriteria().andEqualTo("orderId", orderDetailInfo.getOrderId());
            List<OrderShipping> orderShippings = orderShippingMapper.selectByExample(orderShippingExample);
            OrderShipping orderShipping = orderShippings.get(0);
            OrderShippingDto orderShippingDto = orderConverter.shipping2dto(orderShipping);
            orderDetailInfo.setOrderShippingDto(orderShippingDto);
        }

        Long total = Long.valueOf(orderMapper.selectCountByExample(orderExample));

        orderListResponse.setData(data);
        orderListResponse.setTotal(total);

        return orderListResponse;
    }

    @Override
    public OrderDetailResponse orderDetail(String orderId) {
//     夸oder和ordershipping整合,查询到除goodsList的信息
        OrderDetailResponse orderDetailResponse = orderMapper.selectOrderCrossShipping(orderId);

        List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
        List<OrderItemDto> list = orderItemConverter.orderItem2OrderDetailResponseList(orderItems);

        orderDetailResponse.setGoodsList(list);


        return orderDetailResponse;
    }
}
